// Veoh.js for PSPTube
// Made By JK108

function Veoh_CheckURL( url, option )
{
	if (url.match("http://content.veoh.com/") == "http://content.veoh.com/"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Veoh URL
*/
function Veoh_GetURL( url, option )
{
	return url;
}

function Veoh_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;		// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 20;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(PSPTube.VeohFamilyFilter == 0 && PSPTube.AdultFilter == 0){var familyfilter = "false";}
	else{var familyfilter = "true";}
	if(result.start < perpage){
	var contents = GetContents( "http://www.veoh.com/rest/v2/execute.xml?method=veoh.search.video&userQuery=" + keyword.replace(/ /g, "+") + "&contentSource=veoh&safe=" + familyfilter + "&maxResults=20&offset=0&apiKey=33329B06-1A46-C265-8A85-6AA2F31734CE" );
	} else{
	var num = result.start - 1;
	var contents = GetContents( "http://www.veoh.com/rest/v2/execute.xml?method=veoh.search.video&userQuery=" + keyword.replace(/ /g, "+") + "&contentSource=veoh&safe=" + familyfilter + "&maxResults=20&offset=" + num + "&apiKey=33329B06-1A46-C265-8A85-6AA2F31734CE" );
	}
	if(contents == null) { return null; /* failed to get page */ }


	var pos = contents.indexOf("\" items=\"");
	contents = contents.substring(pos + 9, contents.length);
	pos = contents.indexOf("\" numItems=\"");
	var total = contents.substring(0, pos).replace(/,/g, "");
	result.total = total;


	while(1){
	pos = contents.indexOf("<video videoId=");

	if(pos != -1){
		contents = contents.substring(pos + 15, contents.length);
		//Get URL
		var url = contents.match(/\sfullPreviewHashPath\=\"(.*?)\"/);
		var url = url[1];
		if(url.match(".fll") == ".fll"){url = "";}
		//Get Thumbnail
		var thumb_url = contents.match(/\sfullMedResImagePath\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/\stitle\=\"(.*?)\"/);
		var title = ImproveText(title[1]);
		//Length
		var length = contents.match(/\slength\=\"(.*?)\"/);
		var length = length[1];
		if(length.match(/(.*?) hr (.*?) min/)){
			var hrs = parseInt(RegExp.$1);
			var mins = parseInt(RegExp.$2);
			var secs = 0;
		} else if(length.match(/(.*?) min (.*?) sec/)){
			var hrs = 0;
			var mins = parseInt(RegExp.$1);
			var secs = parseInt(RegExp.$2);
		} else if(length.match(/(.*?) sec/)){
			var hrs = 0;
			var mins = 0;
			var secs = parseInt(RegExp.$1);
		}
		var length = secs + (mins * 60) + (hrs * 3600);
		//Description
		pos = contents.indexOf("description=\"");
		var description = contents.substring(pos + 13, contents.length);
		pos = description.indexOf("\"");
		description = ImproveText(description.substring(0, pos));
		if(description == null || description == ""){description = title;}
		description = splitTo2Lines(description, 60);
		//Views
		var views = contents.match(/\sviews\=\"(.*?)\"/);
		var views = parseInt(views[1]);
		//Rating
		var rating = contents.match(/\srating\=\"(.*?)\"/);
		var rating = parseFloat(rating[1]);
		//Votes
		var votes = contents.match(/\snumRatingVotes\=\"(.*?)\"/);
		var votes = parseInt(votes[1]);
		//Comments
		var comments = contents.match(/\snumOfComments\=\"(.*?)\"/);
		var comments = parseInt(comments[1]);
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = parseInt(length);
		info.Description = description;
		info.ViewCount = views;
		info.RatingAvg = rating;
		info.RatingCount = votes;
		info.CommentCount = comments;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );

		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var Veoh = new Object();
Veoh.Name          = "Veoh";		/* Service name */
Veoh.Description   = "Veoh";		/* Service description */
Veoh.SearchDesc    = "Veoh";		/* Service search screen description */
Veoh.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
Veoh.CheckURL      = Veoh_CheckURL;	/* CheckURL function */
Veoh.GetURL        = Veoh_GetURL;	/* GetURL function */
Veoh.Search        = Veoh_Search;	/* Search function */

// add Veoh to site list
SiteList.push( Veoh );
